package com.example.app2024.animate;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.app2024.R;

public class AnimateActivity extends AppCompatActivity
{
      private AnimationDrawable animationDrawable;
      private static final String TAG = "PropertyActivity";
      TextView textView;

      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
            super.onCreate(savedInstanceState);
            EdgeToEdge.enable(this);
            setContentView(R.layout.activity_animate);
            ViewCompat.setOnApplyWindowInsetsListener(
                    findViewById(R.id.main), (v, insets) -> {
                          Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
                          v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
                          return insets;
                    });

            View view = findViewById(R.id.frameani);
            animationDrawable = (AnimationDrawable) view.getBackground();
            textView = findViewById(R.id.tv_numValue);
            // animationDrawable.setOneShot(true);
      }

      public void onClick(View view)
      {
            if (view.getId() == R.id.btnStart)
            {
                  animationDrawable.start();
            }
            else if (view.getId() == R.id.btnStop)
            {
                  animationDrawable.stop();
            }

            if (view.getId() == R.id.viewAlphaAnimation)
            {
                  Animation alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha);
                  view.startAnimation(alphaAnimation);
            }
            else if (view.getId() == R.id.viewScaleAnimation)
            {
                  Animation scaleAnimation = AnimationUtils.loadAnimation(this, R.anim.scale);
                  view.startAnimation(scaleAnimation);
            }
            else if (view.getId() == R.id.viewTranslateAnimation)
            {
                  Animation translateAnimation = AnimationUtils.loadAnimation(this, R.anim.translate);
                  view.startAnimation(translateAnimation);
            }
            else if (view.getId() == R.id.viewRotateAnimation)
            {
                  Animation rotateAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate);
                  view.startAnimation(rotateAnimation);
            }
            else if (view.getId() == R.id.viewSetAnimation)
            {
                  Animation setAnimation = AnimationUtils.loadAnimation(this, R.anim.set);
                  view.startAnimation(setAnimation);
            }
            else if (view.getId() == R.id.viewLinear || view.getId() == R.id.viewAccelerate)
            {
                  View viewLinear = findViewById(R.id.viewLinear);
                  View viewAccelerate = findViewById(R.id.viewAccelerate);

                  Animation animationLinear = AnimationUtils.loadAnimation(this, R.anim.translate);
                  Animation animationAccelerate = AnimationUtils.loadAnimation(this, R.anim.translate);
                  animationLinear.setInterpolator(new LinearInterpolator());
                  animationAccelerate.setInterpolator(new AccelerateInterpolator());

                  viewLinear.startAnimation(animationLinear);
                  viewAccelerate.startAnimation(animationAccelerate);
            }
            else if (view.getId() == R.id.viewAlphaAnimation2)
            {
                  Animator alphaAnimator = AnimatorInflater.loadAnimator(this, R.animator.alpha);
                  alphaAnimator.setTarget(view);
                  alphaAnimator.start();
            }
            else if (view.getId() == R.id.viewScaleAnimation2)
            {
                ObjectAnimator.ofFloat(view, "scaleX", 1.0f, 3.0f).start();
            }
            else if (view.getId() == R.id.viewTranslateAnimation2)
            {
                  view.animate().translationX(500f).setDuration(1000).start();
            }
            else if (view.getId() == R.id.viewRotateAnimation2)
            {
                  view.animate().rotation(720).start();
            }
            else if (view.getId() == R.id.viewSetAnimation2)
            {
                  Animator rotateAnimator = ObjectAnimator.ofFloat(view, "rotation", 0, 720);
                  rotateAnimator.setDuration(1000);

                  Animator moveAnimator = ObjectAnimator.ofFloat(view, "x", 0, 500);
                  moveAnimator.setDuration(1000);

                  AnimatorSet set = new AnimatorSet();
                  //                set.playTogether(rotateAnimator, moveAnimator);
                  set.playSequentially(rotateAnimator, moveAnimator);
                  set.start();



                  view.animate().rotation(720).setDuration(1000).start();
                  view.animate().translationX(500).setDuration(1000).setStartDelay(1000).start();
            }
            else if (view.getId() == R.id.btnValueAnimator2)
            {
                  ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 100f);
                  valueAnimator.setInterpolator(new LinearInterpolator());
                  valueAnimator.setDuration(3000);
                  valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
                  {
                        @Override
                        public void onAnimationUpdate(ValueAnimator animation)
                        {
                              float animatedFraction = animation.getAnimatedFraction();
                              float animatedValue = (float) animation.getAnimatedValue();
                              textView.setText( String.format("%.3f  %.3f", animatedFraction, animatedValue));
                        }
                  });
                  valueAnimator.start();
            }
      }
}